﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TableEmanee.Dal;

namespace TableEmanee
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private Int32 ID = -1;

        private MembersEntity fillMembersEntityFromForm()
        {
            MembersEntity members = new MembersEntity();
            members.Name = txtname.Text;
            members.Supervisor = cmbosupervisor.Text;
            members.Magmoaa = txtgroup.Text;
            members.Join_Date = pkrjoin.Value;

            return members;
        }
        private void PublishMembersValueToForm(MembersEntity me)
        {
            ID = me.ID;
            txtname.Text = me.Name;
            cmbosupervisor.Text = me.Supervisor;
            txtgroup.Text = me.Magmoaa;
            pkrjoin.Value = me.Join_Date;


        }
        private void clearAllFields()
        {
            cmbosupervisor.Text = null;
            pkrjoin.Value = DateTime.Today;
            txtname.Text = "";
            txtgroup.Text = "";

        }


        private void btnsave_Click(object sender, EventArgs e)
        {
            if (cmbosupervisor.Text == "" || txtname.Text.Equals(null) || txtname.Text == "" || txtgroup.Equals(null) || txtgroup.Text == "")
                MessageBox.Show("يجب إدخال كل البيانات");
            else
            {
                MembersEntity members = fillMembersEntityFromForm();
                //if (ID > 0)
                //{
                //    motabaa.Id = ID;
                //    Motabaa.Update(motabaa);
                //    MessageBox.Show("تم التعديل بنجاح");
                //    ID = 0;
                //    Showgridview(Motabaa.GetByNamebox("%"));
                //}
                if (Members.insert(members))
                {
                    MessageBox.Show("تم حفظ بنجاح");
                    // Showgridview(Motabaa.GetByNamebox("%"));

                }
                clearAllFields();

            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            clearAllFields();
        }
        private void Showgridview(List<MembersEntity> d)
        {
            gridview.DataSource = d;



        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pkrjoin.Format = DateTimePickerFormat.Custom;
            pkrjoin.CustomFormat = "yyyy";
        }

        private void txtname_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                List<MembersEntity> m = Members.GetByName("%" + txtname.Text + "%");
                if (m.Count == 0)
                {
                    MessageBox.Show("هذه العلبة غير موجوده");
                }
                //else if (m.Count == 1)
                //{
                //    PublishMotabaaValueToForm(m[0]);
                //    btnedit.Enabled = true;
                //    button2.Enabled = true;
                //}
                else
                {
                    Showgridview(m);
                }
            }
        }
    }
}

